#!/usr/bin/python
#encoding=utf-8

import urllib
import datetime
import re
import os

def getImg(url):
	for cp in range(0,10):
		#print 
		print "\n"
		page = urllib.urlopen(url+str(cp));
		if page.getcode() == 200 :
			html = page.read();
			imgre = r'download_url"\s*:"(http://.*?\.jpg)"'
			imglist=re.findall(imgre,html)
			page.close()
			print imglist
			currDate=datetime.datetime.now().strftime("%Y-%m-%d")
			localpath="down/"+currDate+"/"
			if not os.path.exists(localpath) :
				os.mkdir(localpath)
			x=0
			for downfile in imglist:
				try:
					downfile=downfile.replace("\\","")
					print x,downfile
					urllib.urlretrieve(downfile,localpath+str(cp)+("%s.jpg" % x))
					#os.remove("%s.jpg" % x) #删除失败的图片
					x=x+1
				except IOError:
					print downfile,"error"

if __name__ == "__main__":
	#首页
	#getImg("http://pic.baidu.com/channel/listjson?pn=144&rn=36&tag1=%E5%A3%81%E7%BA%B8&tag2=%E5%85%A8%E9%83%A8&tag3=&width=1920&height=1080&ie=utf8&oe=utf-8&image_id=&fr=channel&app=img.browse.channel.wallpaper&t=0.47850765800103545&ic=")

    #美女
	#getImg("http://image.baidu.com/channel/listjson?pn=36&rn=36&tag1=%E5%A3%81%E7%BA%B8&tag2=%E7%BE%8E%E5%A5%B3&tag3=%E5%85%A8%E9%83%A8&width=1920&height=1080&ie=utf8&oe=utf-8&image_id=&fr=channel&app=img.browse.channel.wallpaper&t=0.800766485510394&ic=")
	
	#创意
	getImg("http://image.baidu.com/channel/listjson?pn=36&rn=36&tag1=%E5%A3%81%E7%BA%B8&tag2=%E5%88%9B%E6%84%8F&tag3=%E5%85%A8%E9%83%A8&width=1920&height=1080&ie=utf8&oe=utf-8&image_id=&fr=channel&app=img.browse.channel.wallpaper&t=0.029974389355629683&ic=")